local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local AbsActLogic = require("absAct.AbsActLogic")
local AbsReachRankDB = require("absAct.AbsReachRankDB")
local Broadcast = require("broadcast.Broadcast")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsIceDragonRank = require("absAct.AbsIceDragonRank")
--[[
    abs开服排名类活动 现有[合服活动充值排名]
    local:
        getDayLeftTime()        -- 获得当天剩余时间秒
        getActLeftTime()        -- 得到活动需要用到的时间秒
	public:
		isActive()				-- 激活状态
		isOpen()                -- 活动开启	
		getLeftTime()           -- 得到活动剩余时间	
		getAndSendMsg()			-- 发送活动信息
        rewardQuery()           -- 奖励详情
        onCharge()			    -- 充值回调
		onZeroAll()             -- 零点处理回调
--]]

local function getDayLeftTime()
	local now = os.time()
 	local endTime = Util.getDayStartTime(now) + 86400
    
    return endTime - now
end

local function getActLeftTime(funcID, funcRankType)
    for tempFuncRankType,typeInfo in ipairs(AbsReachRankDB.FUNC_RANK_TYPE_INFOS) do
        if funcRankType == tempFuncRankType then
            if typeInfo.isDayReward then
                return getDayLeftTime()
            else
                local ret, endTime, startTime = AbsActLogic.isStarted(human, funcID)
                if ret == true then
                    return endTime - os.time()
                else
                    return 0
                end
            end        
        end 
    end
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

function getLeftTime(human, YInfo, funcConfig)
    return getActLeftTime(funcConfig.funcID)
end

function getAndSendMsg(human, funcID)
    AbsIceDragonRank.query(human)
	local actLeftTime = getActLeftTime(funcID, funcRankType)
	local maxSendRank = AbsReachRankDB.getMaxSendRank(funcID)

	local msgRet = Msg.gc.GC_ABS_REACH_RANK_QUERY
    msgRet.actId = funcID
    local len = 0
    for funcRankType,typeInfo in ipairs(AbsReachRankDB.FUNC_RANK_TYPE_INFOS) do
        if AbsReachRankDB.getMinRankValue(funcID, funcRankType) then
            len = len + 1
            msgRet.list[len].funcRankType = funcRankType
            msgRet.list[len].leftTime = getActLeftTime(funcID, funcRankType)
             
            AbsReachRankDB.wrapOwnerData(funcID, funcRankType, msgRet.list[len].onwerData, human.db._id)
            for rank = 1,maxSendRank do
                AbsReachRankDB.wrapRankList(funcID, funcRankType, msgRet.list[len].list[rank], rank)
            end
            msgRet.list[len].list[0] = maxSendRank
        end
    end
    msgRet.list[0] = len
	Msg.send(msgRet, human.fd)
end

function rewardQuery(human, funcID, funcRankType)
    if funcRankType == 3 then
        AbsIceDragonRank.queryReward(human)
    else
        local msgRet = Msg.gc.GC_ABS_REACH_RANK_REWARD_QUERY
        AbsReachRankDB.rewardQuery(human, funcID, funcRankType, msgRet)
    end
end

function onCharge(human, price, funcID)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    if funcConfig.param[1] == "topupRank" then  -- 是充值排行榜
        AbsReachRankDB.onValueAdd(human, funcID, price)
    end
end
 
function onZeroAll(funcID)
    AbsReachRankDB.onRewardSend(funcID)
    AbsIceDragonRank.onZero(funcID)
end

function updateDaily(human, funcID)
    AbsReachRankDB.updateDaily(human, funcID)
end